home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / deluge / configmanager.py < prev    next >
Text File  |  2009-06-16  |  4KB  |  116 lines

  1. #
  2. # configmanager.py
  3. #
  4. # Copyright (C) 2007 Andrew Resch <andrewresch@gmail.com>
  5. #
  6. # Deluge is free software.
  7. #
  8. # You may redistribute it and/or modify it under the terms of the
  9. # GNU General Public License, as published by the Free Software
  10. # Foundation; either version 3 of the License, or (at your option)
  11. # any later version.
  12. #
  13. # deluge is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. # See the GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with deluge.    If not, write to:
  20. #     The Free Software Foundation, Inc.,
  21. #     51 Franklin Street, Fifth Floor
  22. #     Boston, MA  02110-1301, USA.
  23. #
  24. #    In addition, as a special exception, the copyright holders give
  25. #    permission to link the code of portions of this program with the OpenSSL
  26. #    library.
  27. #    You must obey the GNU General Public License in all respects for all of
  28. #    the code used other than OpenSSL. If you modify file(s) with this
  29. #    exception, you may extend this exception to your version of the file(s),
  30. #    but you are not obligated to do so. If you do not wish to do so, delete
  31. #    this exception statement from your version. If you delete this exception
  32. #    statement from all source files in the program, then also delete it here.
  33. #
  34.  
  35. #
  36.  
  37.  
  38. import gobject
  39. import os
  40. import os.path
  41.  
  42. import deluge.common
  43. from deluge.log import LOG as log
  44. from deluge.config import Config
  45.  
  46. class _ConfigManager:
  47.     def __init__(self):
  48.         log.debug("ConfigManager started..")
  49.         self.config_files = {}
  50.         self.config_directory = deluge.common.get_default_config_dir()
  51.         # Set a 5 minute timer to call save()
  52.         gobject.timeout_add(300000, self.save)
  53.  
  54.     def __del__(self):
  55.         log.debug("ConfigManager stopping..")
  56.         del self.config_files
  57.  
  58.     def set_config_dir(self, directory):
  59.         """Sets the config directory"""
  60.         if directory == None:
  61.             return
  62.         log.info("Setting config directory to: %s", directory)
  63.         if not os.path.exists(directory):
  64.             # Try to create the config folder if it doesn't exist
  65.             try:
  66.                 os.makedirs(directory)
  67.             except Exception, e:
  68.                 log.warning("Unable to make config directory: %s", e)
  69.  
  70.         self.config_directory = directory
  71.  
  72.     def get_config_dir(self):
  73.         return self.config_directory
  74.  
  75.     def close(self, config):
  76.         """Closes a config file."""
  77.         try:
  78.             del self.config_files[config]
  79.         except KeyError:
  80.             pass
  81.  
  82.     def save(self):
  83.         """Saves all the configs to disk."""
  84.         for key in self.config_files.keys():
  85.             self.config_files[key].save()
  86.         # We need to return True to keep the timer active
  87.         return True
  88.  
  89.     def get_config(self, config_file, defaults=None):
  90.         """Get a reference to the Config object for this filename"""
  91.         log.debug("Getting config '%s'", config_file)
  92.         # Create the config object if not already created
  93.         if config_file not in self.config_files.keys():
  94.             self.config_files[config_file] = Config(config_file, defaults, self.config_directory)
  95.  
  96.         return self.config_files[config_file]
  97.  
  98. # Singleton functions
  99. _configmanager = _ConfigManager()
  100.  
  101. def ConfigManager(config, defaults=None):
  102.     return _configmanager.get_config(config, defaults)
  103.  
  104. def set_config_dir(directory):
  105.     """Sets the config directory, else just uses default"""
  106.     return _configmanager.set_config_dir(directory)
  107.  
  108. def get_config_dir(filename=None):
  109.     if filename != None:
  110.         return os.path.join(_configmanager.get_config_dir(), filename)
  111.     else:
  112.         return _configmanager.get_config_dir()
  113.  
  114. def close(config):
  115.     return _configmanager.close(config)
  116.